function createME(id, width, fill) {
	return new mEditor(id, width, '字 体', '字 号', '颜 色', 
		'请输入图片地址', '请输入链接地址', fill);
}
// 编辑区id，编辑区宽度，字体，字号，颜色，图片提示，链接提示，填充内容(可为''或null或胜略)
// ff，fz，fc，tip11，tip12 用于显示，区分不同的语言
var mEditor = function(id, width, fn, fz, fc, tip11, tip12, fill) {
	this.name = 'mEditor';
	this.resource = '';

	this.id = 'mEditor_' + id;	// id前缀
	this.width = width;			// 编辑区宽度
	this.fn = fn;
	this.fz = fz;
	this.fc = fc;
	this.tip11 = tip11;
	this.tip12 = tip12;
	this.fill = (fill?fill:'');

	this.ie = true;				// IE
	this.charset = '';			// 文档编码
	this.meWin = '';			// 编辑区窗体对象
	this.meDoc = '';			// 编辑区文档对象
	this.init();
};

mEditor.prototype = {
'initres': function() {
	this.resource += (
	'<div class="mEditor">' +
	'	<div>' +
	'		' + this.fn +
	'		<select id="'+this.id+'_1" mecmd="fontname">' + /*
	'			<option value=""></option>'*/
	'			<option value="宋体">宋体</option>' +
	'			<option value="楷体_GB2312">楷体_GB2312</option>' +
	'			<option value="新宋体">新宋体</option>' +
	'			<option value="黑体">黑体</option>' +
	'			<option value="隶书">隶书</option>' +
	'			<option value="Andale Mono">Andale Mono</option>' +
	'			<option value="Arial">Arial</option>' +
	'			<option value="Arial Black">Arial Black</option>' +
	'			<option value="Book Antiqua">Book Antiqua</option>' +
	'			<option value="Century Gothic">Century Gothic</option>' +
	'			<option value="Comic Sans MS">Comic Sans MS</option>' +
	'			<option value="Courier New">Courier New</option>' +
	'			<option value="Georgia">Georgia</option>' +
	'			<option value="Impact">Impact</option>' +
	'			<option value="Tahoma">Tahoma</option>' +
	'			<option value="Times New Roman">Times New Roman</option>' +
	'			<option value="Trebuchet MS">Trebuchet MS</option>' +
	'			<option value="sans-serif">sans-serif</option>' +
	'			<option value="Script MT Bold">Script MT Bold</option>' +
	'			<option value="Stencil">Stencil</option>' +
	'			<option value="Verdana">Verdana</option>' +
	'			<option value="Lucida Console">Lucida Console</option>' +
	'		</select>' +
	'		&nbsp;' + this.fz +
	'		<select id="'+this.id+'_2" mecmd="fontsize">' +/*
	'			<option value=""></option>' +*/
	'			<option value="1">1</option><option value="2">2</option><option value="3" selected>3</option>' +
	'			<option value="4">4</option><option value="5">5</option><option value="6">6</option>' +
	'		</select>' +
	'		&nbsp;' + this.fc +
	'		<select class="mEditorColor" id="'+this.id+'_3" mecmd="ForeColor">' +/*
	'			<option value="">&nbsp; &nbsp; &nbsp;  &nbsp;</option>' +*/
	'			<option value="black" class="mEditor_black">black</option>' +
	'			<option value="skyblue" class="mEditor_skyblue">skyblue</option>' +
	'			<option value="royalblue" class="mEditor_royalblue">royalblue</option>' +
	'			<option value="blue" class="mEditor_blue">blue</option>' +
	'			<option value="darkblue" class="mEditor_darkblue">darkblue</option>' +
	'			<option value="orange" class="mEditor_orange">orange</option>' +
	'			<option value="orangered" class="mEditor_orangered">orangered</option>' +
	'			<option value="crimson" class="mEditor_crimson">crimson</option>' +
	'			<option value="red" class="mEditor_red">red</option>' +
	'			<option value="firebrick" class="mEditor_firebrick">firebrick</option>' +
	'			<option value="darkred" class="mEditor_darkred">darkred</option>' +
	'			<option value="green" class="mEditor_green">green</option>' +
	'			<option value="limegreen" class="mEditor_limegreen">limegreen</option>' +
	'			<option value="seagreen" class="mEditor_seagreen">seagreen</option>' +
	'			<option value="teal" class="mEditor_teal">teal</option>' +
	'			<option value="deeppink" class="mEditor_deeppink">deeppink</option>' +
	'			<option value="tomato" class="mEditor_tomato">tomato</option>' +
	'			<option value="coral" class="mEditor_coral">coral</option>' +
	'			<option value="purple" class="mEditor_purple">purple</option>' +
	'			<option value="indigo" class="mEditor_indigo">indigo</option>' +
	'			<option value="burlywood" class="mEditor_burlywood">burlywood</option>' +
	'			<option value="sandybrown" class="mEditor_sandybrown">sandybrown</option>' +
	'			<option value="sienna" class="mEditor_sienna">sienna</option>' +
	'			<option value="chocolate" class="mEditor_chocolate">chocolate</option>' +
	'			<option value="silver" class="mEditor_silver">silver</option>' +
	'		</select>' +
	'	</div>' +
	'   <ul class="mEditorToolbar">' +
	'	<li><a id="'+this.id+'_4" mecmd="bold" href="#" alt="" class="mEditorButtonBold"></a></li>' +
	'	<li><a id="'+this.id+'_5" mecmd="italic" href="#" alt="" class="mEditorButtonItalic"></a></li>' +
	'	<li><a id="'+this.id+'_6" mecmd="underline" href="#" alt="" class="mEditorButtonUnderline"></a></li>' +
	'	<li><a id="'+this.id+'_7" mecmd="JustifyLeft" href="#" alt="" class="mEditorButtonJustifyLeft"></a></li>' +
	'	<li><a id="'+this.id+'_8" mecmd="JustifyCenter" href="#" alt="" class="mEditorButtonJustifyCenter"></a></li>' +
	'	<li><a id="'+this.id+'_9" mecmd="JustifyRight" href="#" alt="" class="mEditorButtonJustifyRight"></a></li>' +
	'	<li><a id="'+this.id+'_11" mecmd="insertimage" mearg="'+this.tip11+'" href="#" alt="" class="mEditorButtonImage"></a></li>' +
	'	<li><a id="'+this.id+'_12" mecmd="createlink" mearg="'+this.tip12+'" href="#" alt="" class="mEditorButtonLink"></a></li>' +
	'	<li><a id="'+this.id+'_10" mecmd="unlink" href="#" alt="" class="mEditorButtonUnlink"></a></li>' +
	'   </ul>' +
	'   <div style="clear:both;"></div>' +
	'	<div class="mEditorCont">'+
	'	<iframe marginwidth="0" marginheight="0" frameborder="0" id="'+this.id+'" ' +
	'		onfocus="this.style.borderColor=\'#990066\';this.style.backgroundColor=\'#FFEEFF\'"' +
	'		onblur="this.style.borderColor=\'#CCCCCC\';this.style.backgroundColor=\'#FFFFFF\'"' +
	'		class="mEditorContIF" />' +
	'	</iframe>' +
	'	</div>'+
	'</div>'
	);
},
'initme': function() {
	this.meDoc.designMode = 'On';
	this.meDoc.contentEditable = true;
	this.meDoc.open();
	this.meDoc.writeln('<html><head></head><body>');
	this.meDoc.writeln(this.fill);
	this.meDoc.write("</body></html>");
	this.meDoc.close();

	var self = this;
	var ipState = function() { self.checkIP(self, false); return false; };
	if(this.ie) {
		// this.meDoc.attachEvent('onclick', this.checkIP);
		this.meDoc.attachEvent('onmouseup', ipState);
		this.meDoc.attachEvent('onkeyup', ipState);
	} else {
		// this.meDoc.addEventListener('click', this.checkIP, false);
		this.meDoc.addEventListener('mouseup', ipState, false);
		this.meDoc.addEventListener('keyup', ipState, false);
	}
	// this.meDoc.charset = this.charset;
	// this.focus();
},
'initcmd': function() {
	var self = this;
	for(i=1; i<4; i++) {
		var obj = self.$(i);
		obj.meDoc = self.meDoc;
		if(self.ie) obj.attachEvent('onchange', self.execs);
		else obj.addEventListener('change', self.execs, false);
	}
	for(i=4; i<13; i++) {
		var b = self.$(i);
		b.meDoc = self.meDoc;
		b.hd = (i<11 ? self.execb : self.exect);
		b.onclick = function() {
			if(self.classExists('on', this.className)) {
				this.className = self.removeClass('on', this.className);
			} else {
				this.className = self.addClass('on', this.className);
			}
			this.hd();
			self.checkIP(self, false);
			self.focus();
			return false;
		};
	}
},
'init': function() {
	if(navigator['appName'].indexOf('Microsoft')==-1) this.ie = false;
	this.charset = (this.ie?document.defaultCharset:document.characterSet);
	// this.prefix += (Math.round(Math.random()*10000)+'_');
	this.initres();
	this.generate();

	this.meWin = document.getElementById(this.id).contentWindow;
	this.meDoc = this.meWin.document;
	this.initme();

	this.initcmd();
},
'generate': function() {
	document.write(this.resource);
	document.getElementById(this.id).style.width = this.width+'px';
},
'execb': function() {
	var obj = (this.event?this.event.srcElement:this);
	obj.meDoc.execCommand(obj.getAttribute('mecmd'), false, null);
},
'execs': function() {
	var obj = (this.event?this.event.srcElement:this);
	obj.meDoc.execCommand(obj.getAttribute('mecmd'), false, obj.value);
},
'exect': function() {
	var obj = (this.event?this.event.srcElement:this);
	var url = prompt(obj.getAttribute('mearg'), "http://");
	if(url) obj.meDoc.execCommand(obj.getAttribute('mecmd'), false, url);
},
'checkIP': function(me, flag) { // IP: Insertion Point

	if(!flag) {
		setTimeout(function(){me.checkIP(me, true);return true;}, 100);
		return false;
	}
	// var obj = (this.event?this.event.srcElement:this);

	var frm = me.meWin;
	var doc = me.meDoc;
	var slc = null;
	var rng = null;
	var pNode = null;

	/* ****** to do ****** */
	// 插入点 + 选择
	// 空选（插入点）处的格式化问题
	// IE：空格问题
	// FF和IE代码兼容问题

	if (doc.selection) {
		slc = doc.selection;
		rng = slc.createRange();
		try { pNode = rng.parentElement(); } catch(e) { return false; }
	} else {
		try { slc = frm.getSelection();} catch(e) { return false; }
		rng = slc.getRangeAt(0);
		// pNode = rng.commonAncestorContainer;
		var an = slc.anchorNode;	var fn = slc.focusNode;
		if(an.nodeType == 3) an = an.parentNode;
		if(fn.nodeType == 3) fn = fn.parentNode;
		while(an.nodeName.toLowerCase()!='body') {
			var tmp = fn;
			while(tmp.nodeName.toLowerCase()!='body' && tmp!=an) {
				tmp = tmp.parentNode;
			}
			if(tmp == an) break;
			an = an.parentNode;
		}
		pNode = an;
	}
	// while (pNode.nodeType == 3) { pNode = pNode.parentNode; }


	// ff:	div, span, <br>, a, img, font[size]	+	style
	//		text-align: center; font-weight: bold; font-style: italic; text-decoration: underline;
	// ie:	p, strong, u, em, img, font[face,size,color]	+	大写
	//		align=center

	var state = me.initState();
	var nName = pNode.nodeName.toLowerCase();
	if(nName == 'img') {
		state.entity = 'image';
		if(pNode.parentNode.nodeName.toLowerCase() == 'a') {
			state.link = true;
		}
	} else if(nName == 'a') {
		state.link = true;
		state.color = 'blue';
	}
	if(me.ie) {
		if(me.cstyle(pNode, 'fontWeight')=='700') state.bold = true;
		if(me.cstyle(pNode, 'fontStyle')=='italic') state.italic = true;
		if(me.cstyle(pNode, 'textDecoration')=='underline') state.underline = true;
	} else {
		if(me.cstyle(pNode, 'fontWeight')=='bold') state.bold = true;
		if(me.cstyle(pNode, 'fontStyle')=='italic') state.italic = true;
		if(me.cstyle(pNode, 'textDecoration')=='underline') state.underline = true;
	}
	state.fontsize = me.cstyle(pNode, 'fontSize');
	if(state.fontsize.length>1) {
		var fss = { '12': 3, '10': 1, '13': 2, '16': 3, '18': 4, '24': 5, '32': 6 };
		state.fontsize = fss[parseInt(state.fontsize)];
	}
	// if(state.fontsize == '12pt') state.fontsize = 3;
	state.align = me.cstyle(pNode, 'textAlign');
	if(state.align == 'start') state.align = 'left';	// FF
	state.fontfamily = me.cstyle(pNode, 'fontFamily');
	state.color = me.rgb2name(me.cstyle(pNode, 'color'));

	me.setState(state);

	return true;
},
'initState': function() {
	return {
		'entity': 'text'/*image*/,  'link': false, 'align': ''/*left,right,center*/, 
		'bold': false, 'italic': false, 'underline': false, 
		'fontfamily': ''/*宋体,Andale Mono*/, 'fontsize': 0/*1-3-6*/, 'color': ''/*black*/
	};
},
'setState': function(state) {
	var me = this;

	var i = 0;
	for(i=4; i<13; i++) {
		var cmd = me.$(i);
		cmd.className = me.removeClass('on', cmd.className);
	}

	var align = state.align.toLowerCase();
	var states = [
		state.bold, state.italic, state.underline, 
		align=='left', align=='center', align=='right', 
		false/*unlink*/, state.entity=='image', state.link
	];
	for(i=4; i<13; i++) {
		if(states[i-4]) {
			var b = me.$(i);
			b.className = me.addClass('on', b.className);
		}
	}

	me.$(1).value = state.fontfamily;
	me.$(2).value = state.fontsize;
	me.$(3).value = state.color;
},
'getCont': function() { return this.meDoc.getElementsByTagName('body')[0].innerHTML; },
'selectAll': function() {
	this.meDoc.execCommand('selectall', false, null);
},
'clear': function() {
	this.selectAll();
	this.meDoc.execCommand('delete', false, null);
},
'insert': function(cont, clear) {
	if(clear) this.clear();
	if(this.ie) {
		this.meDoc.selection.createRange().pasteHTML(cont);
	} else {
		this.meDoc.execCommand('inserthtml', false, cont);
		/*var cnt = this.meDoc.createTextNode(cont);
		var sln = this.meWin.getSelection();
		if(sln.toString().length)
			sln.deleteFromDocument();
		sln.getRangeAt(0).insertNode(cnt);*/
	}
	/*try { this.meDoc.execCommand('inserthtml', false, cont); } // FF
	catch(e) { this.meDoc.selection.createRange().text=cont; } // IE*/
},
'focus': function() { this.meWin.focus(); },
'mv2begin': function() {
	var bd = this.meDoc.getElementsByTagName('body')[0];
	if(this.ie) {
		var rng = bd.createTextRange();
		rng.collapse();
		rng.select();
	} else {
		this.meWin.getSelection().collapse(bd, 0);
		this.meWin.scrollTo(0, 0);
	}
},
'$': function(num) { return document.getElementById(this.id+'_'+num); },
'classExists': function(needle, hayStack) { /* Check if a class exists in a string */
	// var hss = new Array('XX', 'XX ', ' XX', 'XX Y', 'Y XX', 'XX YY ZZ', 
	//						'', 'XXY', 'YXX', 'YXXY', 'Y XXY', 'YXX Y', null);
	var regString = "(^| )" + needle + "( |$)";
	return new RegExp(regString).test(hayStack);
},
'removeClass': function(needle, hayStack) { /* Remove a class from a string */
	if(!hayStack) return '';
	var regString = "(^| )" + needle + "( |$)";
	var regExpression = new RegExp(regString);
	return hayStack.replace(regExpression, "");
},
'addClass': function(needle, hayStack) { /* Add a class to a string */
	if(!hayStack) return needle;
	if(this.classExists(needle, hayStack)) return hayStack;
	return hayStack + ' ' + needle;
},
'cstyle': function(obj, prop) {
	if(obj.currentStyle) {
		return obj.currentStyle[prop];
	} else if(window.getComputedStyle) {
		prop = prop.replace(/([A-Z])/g, "-$1");
		prop = prop.toLowerCase();
		return obj.ownerDocument.defaultView.getComputedStyle(obj, "").getPropertyValue(prop);
	}
	return null;
},
'rgb2name': function(rgb) {
	var colors = {
		'rgb(0, 0, 0)': 'black',	'rgb(135, 206, 235)': 'skyblue',
		'rgb(65, 105, 225)': 'royalblue','rgb(0, 0, 255)': 'blue',
		'rgb(0, 0, 139)': 'darkblue',	'rgb(255, 165, 0)': 'orange',
		'rgb(255, 69, 0)': 'orangered',	'rgb(220, 20, 60)': 'crimson',
		'rgb(255, 0, 0)': 'red',	'rgb(178, 34, 34)': 'firebrick',
		'rgb(139, 0, 0)': 'darkred',	'rgb(0, 128, 0)': 'green',
		'rgb(50, 205, 50)': 'limegreen','rgb(46, 139, 87)': 'seagreen',
		'rgb(0, 128, 128)': 'teal',	'rgb(255, 20, 147)': 'deeppink',
		'rgb(255, 99, 71)': 'tomato',	'rgb(255, 127, 80)': 'coral',
		'rgb(128, 0, 128)': 'purple',	'rgb(75, 0, 130)': 'indigo',
		'rgb(222, 184, 135)': 'burlywood','rgb(244, 164, 96)': 'sandybrown',
		'rgb(160, 82, 45)': 'sienna',	'rgb(210, 105, 30)': 'chocolate',
		'rgb(192, 192, 192)': 'silver',	'#000000': 'black'
	};
	return colors[rgb]?colors[rgb]:rgb;
}
};



